/*
** exec_cmd.c for  in /home/jousse_f//workspace/SysUnix/ftp
** 
** Made by florian jousseau
** Login   <jousse_f@epitech.net>
** 
** Started on  Wed Apr  4 11:12:14 2012 florian jousseau
** Last update Wed Apr  4 15:58:57 2012 florian jousseau
*/

#include		"server.h"
#include		<dirent.h>

int			ls(char *buf, int cs)
{
  DIR			*ptdir;
  struct dirent		*entre;

  printf("\033[31;01mClient\033[00m : %s\n-> ", buf);
  ptdir = opendir(".");
  if (!ptdir)
    {
      printf("cannot opendir \n");
      return (1);
    }
  while((entre = readdir(ptdir)) != NULL)
    printf("%s\n", entre->d_name);
  closedir(ptdir);
  (void)cs;
  return (0);
}

int			cd(char *buf, int cs)
{
  char			*path;
  int			i;
  int			ret;

  (void)cs;
  i = 0;
  path = malloc(sizeof(char) * 512);
  printf("\033[31;01mClient\033[00m : %s\n", buf);
  if (strlen(buf) == 2)
    ret = chdir(getenv("HOME"));
  else
    {
      while (buf[i + 3] != '\0')
	{
	  path[i] = buf[i + 3];
	  i++;
	}
      ret = chdir(path);
    }
  if (ret != 0)
    printf("Error on chdir.\n");
  free(path);
  return (0);
}

int			pwd(char *buf, int cs)
{
  (void)cs;
  char		        *ret;

  printf("\033[31;01mClient\033[00m : %s\n-> ", buf);
  if ((ret = getcwd(buf, 512)) != NULL)
    printf("%s\n", ret);
  return (0);
}

int			quit(char *buf, int cs)
{
  (void)buf;
  (void)cs;
  printf("\033[31;01mClient\033[00m : %s\n-> this client has left the server\n", buf);
  return (1);
}
